﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.GlobalClass;
using Zocono.WCS.Domain.PlcBytesObject;
using Zocono.WCS.Domain.ProcessModuleDomain;

namespace Zocono.WCS.Domain.EquipmentProcessDomain.PlanarGeneralMessage
{
    /// <summary>
    /// 平面设备通用PLC动作报文信息服务
    /// </summary>
    public class PlanarGeneralPlcActionBOMessageService : IPlanarActionMessageService
    {
        private readonly IRedisHelper _IRedisHelper;
        private readonly IPlanarProcessServiceMapInfoRepository _planarProcessServiceMapInfoRepository;
        private readonly IPlanarGeneralPlcActionProcessServiceFactory _planarGeneralPlcActionProcessServiceFactory;
        private readonly ILogUtil _logUtil;
        private readonly ISnowflake _Snowflake;
        private readonly ITelegramRecordInfoRepository _TelegramRecordInfoRepository;
        public PlanarGeneralPlcActionBOMessageService(IRedisHelper iRedisHelper, IPlanarProcessServiceMapInfoRepository planarProcessServiceMapInfoRepository, IPlanarGeneralPlcActionProcessServiceFactory planarGeneralPlcActionProcessServiceFactory, ILogUtil logUtil, ISnowflake snowflake, ITelegramRecordInfoRepository telegramRecordInfoRepository)
        {
            _IRedisHelper = iRedisHelper;
            _planarProcessServiceMapInfoRepository = planarProcessServiceMapInfoRepository;
            _planarGeneralPlcActionProcessServiceFactory = planarGeneralPlcActionProcessServiceFactory;
            _logUtil = logUtil;
            _Snowflake = snowflake;
            _TelegramRecordInfoRepository = telegramRecordInfoRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="plcActionMessage">plc动作报文</param>
        /// <param name="planarEquipmentDbObject">平面报文</param>
        /// <param name="plcStateMassge"></param>
        /// <returns></returns>
        public async Task ProcessActionMessageAsync(BytesObjectBase plcActionMessage, EquipmentDbObject planarEquipmentDbObject/*, BytesObjectBase plcStateMassge*/)
        {
            var plcMessage = plcActionMessage as PlanarGeneralPlcActionBO;
            var wcsMessage = planarEquipmentDbObject.bytesObjectBases.Find(o => o.ObjectType == "PlanarGeneralWcsActionBO") as PlanarGeneralWcsActionBO;
            if (wcsMessage == null)//PlanarGeneralPlcActionBO
            {
                return;
            }

            string equipmentCode = plcMessage.EquipmentCode;
            short plcActionType = plcMessage.ActionType;
            short wcsActionType = wcsMessage.ActionType;



            var serviceMapInfo = GlobalResource.PlanarProcessServiceMapInfos.FirstOrDefault(p => p.EquipmentCode == equipmentCode &&
            p.PlcActionType == plcActionType && p.WcsActionType == wcsActionType);
            //var serviceMapInfo = _planarProcessServiceMapInfoRepository.GetFirst(p => p.EquipmentCode == equipmentCode &&
            //p.PlcActionType == plcActionType && p.WcsActionType == wcsActionType);//通过设备码，PLC写信号，WCS写信好获取业务类
            if (serviceMapInfo == null)
            {
                //_logUtil.Error($"普通线体设备：设备号({equipmentCode})查找平面设备业务配置PLC握手【{plcActionType}】-WCS握手【{wcsActionType}】未配置！");
                return;
            }
            //按配置信息创建平面正常报文业务处理类 
            var processService = _planarGeneralPlcActionProcessServiceFactory.Create(serviceMapInfo.PlanarProcessServiceName);//根据信号运行相应类
            if (processService == null)
            {
                _logUtil.Error($"普通线体设备：设备号({equipmentCode})创建服务({serviceMapInfo.PlanarProcessServiceName})异常:at PlanarGeneralPlcActionBOMessageService.ProcessActionMessageAsync");
                return;
            }
            //处理对应业务
            await processService.ProcessActionMessageAsync(plcMessage, wcsMessage, planarEquipmentDbObject/*, plcStateMassge*/);
        }
    }
}
